using mock_ehr_lib.Data;
using mock_ehr_lib.Data.LookupServices;
using mock_ehr_lib.Model;
using System.ComponentModel;
using System.Configuration;
namespace mock_ehr_lib
{
    public delegate void UserChanged(User newUser);
    public delegate void PatientChanged(Patient newPatient);
    public class MockEHRApplication
    {
        private User _currentUser;
        private Patient _currentPatient;
        private static MockEHRApplication _instance;

        public UserChanged OnUserChanged;
        [Browsable(false)]
        public PatientChanged OnPatientChanged;
        private readonly IUserRepository _userRepository;
        private readonly IPatientRepository _patientRepository;
        private readonly IOrderRepository _orderRepository;
        private readonly string _infobuttonBaseUrl;

        /// <summary>
        /// Private for singleton impl
        /// </summary>
        private MockEHRApplication()
        {
            var usersFilename = ConfigurationManager.AppSettings["usersFilename"];
            var patientsFilename = ConfigurationManager.AppSettings["patientsFilename"];
            var ordersFilename = ConfigurationManager.AppSettings["ordersFilename"];
            _userRepository = new JsonUserRepository(usersFilename);
            _patientRepository = new JsonPatientRepository(patientsFilename);
            _orderRepository = new JsonOrderRepository(ordersFilename);
            _infobuttonBaseUrl = ConfigurationManager.AppSettings["infobuttonBaseUrl"];
        }

        /// <summary>
        /// Access the sole application instance
        /// </summary>
        public static MockEHRApplication Instance
        {
            get { return _instance ?? (_instance = new MockEHRApplication()); }
        }

        public User CurrentUser
        { 
            get
            {
                return _currentUser;
            } 
            set
            {
                _currentUser = value;
                if (OnUserChanged != null)
                {
                    OnUserChanged(value);
                }
            }
        }

        public Patient CurrentPatient 
        {
            get
            {
                return _currentPatient;
            }
            set
            {
                _currentPatient = value;
                if (OnPatientChanged != null)
                {
                    OnPatientChanged(value);
                }
            }
        }

        public IUserRepository UserRepository
        {
            get { return _userRepository; }
        }

        public IPatientRepository PatientRepository
        {
            get { return _patientRepository; }
        }

        public IOrderRepository OrderRepository
        {
            get { return _orderRepository; }
        }

        public string InfoButtonBaseUrl
        {
            get { return _infobuttonBaseUrl; }
        }
    }
}